﻿using System;
using System.Collections.Generic;
using System.ServiceModel.Activation;

namespace Prompts.Service.PromptService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class PromptSelectionService : IPromptSelectionService
    {
         private readonly IBaseReportParameterService _parameterService;
         private readonly ISelectionParameterValueBuilder _selectionParameterValueBuilder;

         public PromptSelectionService(
            IBaseReportParameterService parameterService
            , ISelectionParameterValueBuilder selectionParameterValueBuilder)
         {
             _selectionParameterValueBuilder = selectionParameterValueBuilder;
             _parameterService = parameterService;
         }

         public SetPromptSelectionsResponse SetPromptSelections(
             string path, 
             IEnumerable<PromptSelectionInfo> promptSelections)
         {
             try
             {
                 var baseReportParameters = _parameterService.GetParametersFor(path);
                 var parameterValues = _selectionParameterValueBuilder.Get(baseReportParameters, promptSelections);
                 var executionId = _parameterService.SetParameters(parameterValues);
                 return new SetPromptSelectionsResponse { ExecutionId = executionId };
             }
             catch (Exception exception)
             {
                 return new SetPromptSelectionsResponse()
                     {
                         ErrorOccured = true,
                         ErrorMessage = exception.Message
                     };
             }
         }
    }
}
